import { defineConfig } from 'vite'
import fs from 'fs-extra'
import path from 'path'
import uni from '@dcloudio/vite-plugin-uni'

function copyFile() {
  return {
    enforce: 'post',
    async writeBundle() {
      await fs.copy(
        path.resolve(__dirname, './src/cloudfunctions'),
        path.join(
          __dirname,
          'dist',
          process.env.NODE_ENV === 'production' ? 'build' : 'dev',
          process.env.UNI_PLATFORM,
          'cloudfunctions'
        )
      )
    },
  }
}

export default defineConfig({
  css: {
    preprocessorOptions: {
      scss: {
        api: 'modern-compiler',
        silenceDeprecations: ['legacy-js-api']
      }
    }
  },
  plugins: [uni(), copyFile()],
})